//
//  Net.swift
//  WanZhou
//
//  Created by xhl_dabaoMac on 2021/11/11.
//

import Foundation
import Alamofire

public class Urls{
    static let news = "news.html"
}



public class Net {
    
    static var format: DateFormatter {
        print("DateFormatter")
        let format = DateFormatter();
        format.dateFormat = "YYYYMMddHHmmssSSS";
        return format;
    };
    
    public static var shared = Net()
    
        
    public static func request(_ convertible : URLConvertible,
                               parameters : [String:Any] = [String:Any](),
                               method: HTTPMethod = .post,
                               complete : @escaping ([String:Any]?,String?) -> () ){
        
        AF.request(convertible,
                   method:method,
                   parameters: parameters).responseJSON {
                     response in
                   
                    switch response.result {
                    case .success(let res):
                        
                        let result = res as! [String : Any]
                        
                        let code = result["code"] as! Int
                        if code == 0 {
                            complete(result,nil)
                        }else{
                            complete(nil,result["message"] as? String)
                        }
                    case .failure(let error):
                        complete(nil,"\(error)")
                    }
                   }
    }
    
    
    
}


extension Dictionary {
    func jsonPrint() {
        let ff = try! JSONSerialization.data(withJSONObject:self, options: [])
        let str = String(data:ff, encoding: .utf8)
        print(str!)
    }
}
